package com.ve.c51.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;


public class BaseActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private ProgressDialog progressDialog;

    public Toolbar initToolbar(@IdRes int id) {
        toolbar = findViewById(id);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        return toolbar;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public void startActivity(Class<? extends AppCompatActivity> activity) {
        super.startActivity(new Intent(this, activity));
    }

    public void showProgress(final boolean show, String text) {
        if (show) {
            if (progressDialog == null) {
                progressDialog = ProgressDialog.show(this, text, "", false, false);
            } else {
                progressDialog.setTitle(text);
                progressDialog.show();
            }
        } else {
            if (progressDialog != null) {
                progressDialog.dismiss();
            }
        }

    }

}
